<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
	}

	protected function renderView($viewName, $data = null)
	{
	    if ($viewName != "login_view") {
			$this->checkLogin();
		}
        $isLoged = false;
        if ($this->session->userdata('logged_in')) {
            $isLoged = true;
        }
        $data['isLoged'] = $isLoged;
		$this->load->view("common/header", $data);
		$this->load->view($viewName, $data);
		$this->load->view("common/footer");
	}

    public function getHtmlFromtemplate($view, $data)
    {
        $html = $this->load->view($view, $data, TRUE);
        return $html;
    }
    
    public function checkLogin()
    {
        if(!$this->session->userdata('logged_in')){
         //If no session, redirect to login page
         redirect('login', 'refresh');
       }
    }

	private function requireLogin()
	{
		$segment = $this->uri->segment(1);
		if($segment=='cron'){
			return;
		}
		$acl = unserialize(ACL);
		$userlogin = $this->session->userdata('userlogin');
		if (in_array($segment, $acl) && !$userlogin) {
			$this->session->set_flashdata('referrer', uri_string());
			redirect(base_url(array('user', 'login')));
		}
	}

	protected function getAgent()
	{
		$agent = '';
		try {
			$agent = $this->input->user_agent();
			if (!$agent) {
				$this->load->library('user_agent');
				if ($this->agent->is_browser()) {
					$agent = $this->agent->browser() . ' ' . $this->agent->version();
				} else {
					if ($this->agent->is_robot()) {
						$agent = $this->agent->robot();
					} else {
						if ($this->agent->is_mobile()) {
							$agent = $this->agent->mobile();
						} else {
							$agent = 'Unidentified User Agent';
						}
					}
				}
			} else {
				$agent = 'Unidentified User Agent';
			}
		} catch (Exception $e) {
			$agent = 'Unidentified User Agent';
		}
		return $agent;
	}

	protected function encodeURL($sym)
	{
		if (strpos($sym, '&')) {
			$sym = str_replace('&', '%26', $sym);
		}
		if (strpos($sym, ' ')) {
			$sym = str_replace(' ', '%20', $sym);
		}
		return $sym;
	}
}
?>